package com.tanhua.mongodbManager.controller;

import com.tanhua.mongodbManager.domain.PageResult;
import com.tanhua.mongodbManager.domain.mongod.DynamicValidateForm;
import com.tanhua.mongodbManager.domain.vo.OptionsVo;
import com.tanhua.mongodbManager.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName -> QuctionsController
 * @Description
 * @Author OHC
 * @Date 2021/8/19 星期四
 */
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 查询所有的问题
     */
    @PostMapping("/findAllQuestion")
    public ResponseEntity<Object> findAllQuestion(@RequestBody Map<String, Object> param) {
        Integer page = (Integer) param.get("page");
        if (Objects.isNull(page)||page<0) {
            page = 1;
        }
        Integer pagesize = (Integer) param.get("pagesize");
        if (Objects.isNull(pagesize)||pagesize<0) {
            pagesize = 5;
        }
        PageResult pageResult = questionService.findAllQuestion(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 搜索问题
     */
    @RequestMapping("/search")
    public ResponseEntity<Object> searchQuestion(@RequestBody Map<String, Object> param) {
        Integer page = (Integer) param.get("page");
        if (Objects.isNull(page)||page<0) {
            page = 1;
        }
        Integer pagesize = (Integer) param.get("pagesize");
        if (Objects.isNull(pagesize)||pagesize<0) {
            pagesize = 5;
        }
        String queryQuestion = (String) param.get("query");
        PageResult pageResult = questionService.searchQuestion(page, pagesize,queryQuestion);
        return ResponseEntity.ok(pageResult);
    }



    /**
     * 添加问题
     */
    @PostMapping("/addAndModify")
    public ResponseEntity<Object> addAndModify(@RequestBody Map<String, Object> param) {
        //判断添加选项的时候选项是否为空
        ArrayList<Map<String, Object>> optionsList = (ArrayList<Map<String, Object>>) param.get("options");
        Map<String, Object> resultMap = new HashMap<>();
        if (optionsList.size() == 0) {
            resultMap.put("success", false);
            resultMap.put("message", "选项不能为空");
            return ResponseEntity.ok(resultMap);
        }
        Long row = questionService.addAndModify(param);
        return getObjectResponseEntity(row, "添加成功", "添加失败");
    }

    /**
     * 删除问题
     */
    @GetMapping("/removeQuestion")
    public ResponseEntity<Object> removeQuestion(String questionId) {
        Long row = questionService.removeQuestion(questionId);
        return getObjectResponseEntity(row, "删除成功", "删除失败");
    }


    /**
     * 返回数据
     */
    private ResponseEntity<Object> getObjectResponseEntity(Long row, String message, String errorMessage) {
        Map<String, Object> resultMap = new HashMap<>();
        if (row > 0) {
            resultMap.put("success", true);
            resultMap.put("message", message);
            return ResponseEntity.ok(resultMap);
        }
        resultMap.put("success", false);
        resultMap.put("message", errorMessage);
        return ResponseEntity.ok(resultMap);
    }

    /**
     * 编辑回显
     */
    @GetMapping("/editQuestion")
    public ResponseEntity<Object> editQuestion(String questionId) {
        DynamicValidateForm dynamicValidateForm = questionService.editQuestion(questionId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("echoData", dynamicValidateForm);
        return ResponseEntity.ok(resultMap);
    }

    /**
     * 修改数据
     */
    @PostMapping("/fixTheProblem")
    public ResponseEntity<Object> fixTheProblem(@RequestBody DynamicValidateForm dynamicValidateForm) {
        ArrayList<OptionsVo> options = dynamicValidateForm.getOptions();
        Map<String, Object> resultMap = new HashMap<>();
        if (options.size() == 0) {
            resultMap.put("success", false);
            resultMap.put("message", "选项不能为空");
            return ResponseEntity.ok(resultMap);
        }
        Long row = questionService.fixTheProblem(dynamicValidateForm);
        return getObjectResponseEntity(row, "更新成功", "请修改需要更新的问题");
    }

}
